package com.zuo.demo01.controller.blogs;


import com.zuo.demo01.pojo.Blog;
import com.zuo.demo01.pojo.Comment;
import com.zuo.demo01.pojo.Tag;
import com.zuo.demo01.service.BlogService;
import com.zuo.demo01.service.CommentService;
import com.zuo.demo01.service.ConfigService;
import com.zuo.demo01.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DetailController {

    @Autowired
    private BlogService blogService;


    @Autowired
    private ConfigService configService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CommentService commentService;


    @RequestMapping("/blog/detail/{id}")
    public String blog_detail(@PathVariable("id") Integer id, Model model){

        // 根据id增加浏览量
        blogService.addViews(id);

        return comment_page(1,5,id,model);
    }


    @RequestMapping("/comment/insert")
    @ResponseBody
    public String comment_insert(Comment comment, @RequestParam("verifyCode") String verifyCode, @RequestParam("blogId") Integer blogId, HttpSession session){

        String verify = session.getAttribute("verifyCode")+"";
        if(!verify.equalsIgnoreCase(verifyCode)){
            return "no_verify";
        }

        Map<String,Object> map = new HashMap<>();
        map.put("commentator",comment.getCommentator());
        map.put("email",comment.getEmail());
        map.put("url",comment.getUrl());
        map.put("content",comment.getContent());
        map.put("blogId",blogId);
        int result = commentService.insert(map);
        if(result==1){
            return "success";
        }else{
            return "error";
        }


    }


    @RequestMapping("/comment/page")
    public String comment_page(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit,@RequestParam("blogId") Integer blogId,Model model){
        // id获取博客
        Blog blog = blogService.getByIdInHTML(blogId);
        model.addAttribute("blog",blog);



        // 获取博客对应标签
        List<Tag> tags = tagService.findByBlog(blogId);
        model.addAttribute("tags",tags);

        //获取博客对应评论
        Map<String,Object> map = new HashMap<>();
        map.put("blogId",blogId);
        map.put("startIndex",(page-1)*limit);
        map.put("pageSize",limit);
        List<Comment> comments = commentService.findByBlog(map);
        model.addAttribute("comments",comments);

        int count_commmets = commentService.countByBlog(blogId);
        model.addAttribute("count_commmets",count_commmets);

        Map<String, String> configs = configService.findAll();
        model.addAttribute("configs",configs);

        model.addAttribute("curr",page);

        return "blogs/blog";
    }






}
